package com.forfunnet.minjian.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.forfunnet.minjian.R;
import com.jakewharton.salvage.RecyclingPagerAdapter;
import com.applidium.shutterbug.FetchableImageView;
import com.viewpagerindicator.IconPagerAdapter;

import java.util.List;

/**
 * Created by wgh on 2015/1/23.
 */
public class AdsImagePagerAdapter extends RecyclingPagerAdapter implements IconPagerAdapter
{
    private Context context;
    private List<ItemData>   mDataList;

    protected int imageDestWidth = -1;
    protected int imageDestHeight = -1;

    public static class ItemData
    {
        public String mImageUrl;
        public String mContent;
        public String mUrl;
    }

    private boolean       isInfiniteLoop;

    private static class ListItemView
    {
        public ImageView adsImg;
        public TextView adsContent;
    }

    public AdsImagePagerAdapter(Context context, List<ItemData> dataList)
    {
        this.context = context;
        this.mDataList = dataList;
        isInfiniteLoop = false;
    }

    public boolean setImageDestSize(int width, int height) {
        if (width <= 0 || height <= 0) {
            return false;
        }
        imageDestWidth = width;
        imageDestHeight = height;
        return true;
    }

    @Override
    public int getIconResId(int index) {
        return R.drawable.pager_indicator;
    }

    @Override
    public int getCount() {
        // Infinite loop
        return isInfiniteLoop ? Integer.MAX_VALUE : mDataList.size();
    }

    /**
     * get really position
     *
     * @param position
     * @return
     */
    private int getPosition(int position) {
        return isInfiniteLoop ? position % mDataList.size() : position;
    }

    public interface OnAdsClickListener
    {
        void onClick(Uri uri);
    }

    private OnAdsClickListener mAdsClickListener;
    public void setOnAdsClickListener(OnAdsClickListener listener)
    {
        mAdsClickListener = listener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup container)
    {
        if (convertView == null)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_ads_image_pager, null);
        }

        FetchableImageView adsImg = (FetchableImageView)convertView.findViewById(R.id.image);
        TextView adsContent = (TextView)convertView.findViewById(R.id.content);

        final ItemData data = mDataList.get(getPosition(position));

        adsImg.setScaleType(ImageView.ScaleType.CENTER_CROP);
        if (imageDestWidth <= 0 || imageDestHeight <= 0) {
            adsImg.setImage(data.mImageUrl);
        } else {
            adsImg.setImage(data.mImageUrl, imageDestWidth, imageDestHeight);
        }

        adsContent.setText(data.mContent);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.mUrl != null) {
                    Uri uri = Uri.parse(data.mUrl);
                    String scheme = uri.getScheme();
                    if (scheme != null && scheme.equals("http")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        context.startActivity(intent);
                    } else if (mAdsClickListener != null) {
                        mAdsClickListener.onClick(uri);
                    }
                }
            }
        });
        return convertView;
    }

    /**
     * @return the isInfiniteLoop
     */
    public boolean isInfiniteLoop() {
        return isInfiniteLoop;
    }

    /**
     * @param isInfiniteLoop the isInfiniteLoop to set
     */
    public AdsImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
        this.isInfiniteLoop = isInfiniteLoop;
        return this;
    }
}
